import React from 'react';
import style from './login.module.scss'
import  {Form,Button,Checkbox,Input,message} from 'antd'
import request from "../../http/request";
import { useNavigate} from 'react-router-dom'
const Login = () => {
    const navigate=useNavigate()
    const onFinish = (values: any) => {
        console.log('Success:', values);
        request.get("/api/login",{
            params:values
        }).then(({data})=>{
            console.log("data",data)
            if(data.code===200){
                sessionStorage.setItem("token",data.token)
                message.success('登录成功');
                navigate('/welcome')
            }else {
                message.error('登录失败');
            }
        })
    };
    const registerUser=()=>{
        navigate("/register")
    }
    const onFinishFailed = (errorInfo: any) => {
        console.log('Failed:', errorInfo);
    };
    return (
        <div className={style.main}>
             <div className={style.loginBox}>
                <div className={style.title}>欢迎登录</div>
                <Form
                    name="basic"
                    labelCol={{ span: 7 }}
                    wrapperCol={{ span: 13 }}
                    style={{ maxWidth: 500 }}
                    initialValues={{ remember: true }}
                    onFinish={onFinish}
                    onFinishFailed={onFinishFailed}
                    autoComplete="off"
                >
                    <Form.Item
                        label="用户名"
                        name="username"
                        rules={[{ required: true, message: '请输入用户名!' }]}
                    >
                        <Input />
                    </Form.Item>

                    <Form.Item
                        label="密码"
                        name="password"
                        rules={[{ required: true, message: '请输入密码!' }]}
                    >
                        <Input.Password />
                    </Form.Item>

                    <Form.Item name="remember" valuePropName="checked" wrapperCol={{ offset: 7, span: 16 }}>
                        <Checkbox>记住我</Checkbox>
                    </Form.Item>

                    <Form.Item wrapperCol={{ offset: 8, span: 16 }}>
                        <Button  style={{width:"150px"}}  type="primary"  htmlType="submit">登录</Button>

                    </Form.Item>
                    <Form.Item wrapperCol={{ offset: 8, span: 16 }}>
                        <Button  onClick={()=>registerUser()}    style={{width:"150px"}}  type="primary"  >注册</Button>

                    </Form.Item>

                </Form>







        </div>
        </div>

    );
};

export default Login;